package lanou3g.myfirstapp36kr.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import it.sephiroth.android.library.picasso.Picasso;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.entity.SearchActivityLvEntity;

/**
 * Created by dllo on 16/8/9.
 */
public class ActivitySearchLvAdapter extends BaseAdapter {
    private SearchActivityLvEntity entity;
    private Context context;
    private Date date ;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH:mm:ss");

    public ActivitySearchLvAdapter(Context context) {
        this.context = context;
    }

    public void setEntity(SearchActivityLvEntity entity) {
        this.entity = entity;
    }

    @Override
    public int getCount() {
        return entity == null ? 0 : entity.getData().getData().size();
    }

    @Override
    public Object getItem(int i) {
        return entity.getData().getData().get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_search_activity_lv,null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (entity != null){
            Picasso.with(context).load(entity.getData().getData().get(i).getFeatureImg()).into(viewHolder.imageViewFeature);
            viewHolder.textViewTitle.setText(entity.getData().getData().get(i).getTitle());
            viewHolder.textViewName.setText(entity.getData().getData().get(i).getUser().getName());
            date = new Date(entity.getData().getData().get(i).getPublishTime());
            String stringDate = dateFormat.format(date);
            viewHolder.textViewTime.setText(stringDate);
        }
        return view;
    }
    private class ViewHolder{
        private ImageView imageViewFeature;
        private TextView textViewTitle;
        private TextView textViewName;
        private TextView textViewTime;

        public ViewHolder(View view) {
            imageViewFeature = (ImageView) view.findViewById(R.id.iv_feature);
            textViewTitle = (TextView) view.findViewById(R.id.tv_title);
            textViewName = (TextView) view.findViewById(R.id.tv_name);
            textViewTime = (TextView) view.findViewById(R.id.tv_time);
        }
    }
}
